package io.renren.modules.seller.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.seller.entity.BrandEntity;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌
 *
 * @author Mark  15373178086@163.com
 * @date 2020-02-16 11:31:02
 */
@Slf4j
@RestController
@RequestMapping("manage/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 返回全部列表
     *
     * @return TbBrands
     */
    @RequestMapping("/findAll")
    public List<BrandEntity> findAll() {
        return brandService.findAll();
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody BrandEntity brandEntity, String page, String limit) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", limit);
        params.put("name", brandEntity.getName());
        params.put("firstChar", brandEntity.getFirstChar());
        log.info("brand search params:{}", params);
        return brandService.findPage(params);
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestParam Map<String, Object> params) {
        log.info("brand findPage params:{}", params);
        return brandService.findPage(params);
    }

    /**
     * 增加
     *
     * @param brand brand
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody BrandEntity brand) {
        try {
            brandService.save(brand);
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 修改
     *
     * @param brand brand
     * @return Result
     */
    @RequestMapping("/update")
    public Result update(@RequestBody BrandEntity brand) {
        try {
            brandService.updateById(brand);
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 获取实体
     *
     * @param id id
     * @return BrandEntity
     */
    @RequestMapping("/findOne")
    public BrandEntity findOne(Long id) {
        return brandService.getById(id);
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return Result
     */
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            brandService.removeByIds(Arrays.asList(ids));
            return Result.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    @RequestMapping("/selectOptionList")
    public List<Map> selectOptionList() {
        return brandService.selectOptionList();
    }

}
